<template>
	<view class="collection-container">
		<!-- 顶部区域 -->
		<view class="header">
			<view class="header-title">
				<text class="title-text">我的收藏</text>
				<text class="sub-title">共 {{totalItems}} 个内容</text>
			</view>
			<view class="header-action" @click="toggleEditMode">
				<text>{{isEditMode ? '完成' : '管理'}}</text>
			</view>
		</view>
		
		<!-- 分类导航 -->
		<view class="category-tabs">
			<scroll-view scroll-x class="tabs-scroll" show-scrollbar="false">
				<view 
					class="tab-item" 
					:class="{ active: activeTab === index }"
					v-for="(tab, index) in tabs" 
					:key="index"
					@click="switchTab(index)"
				>
					<text>{{tab.name}}</text>
				</view>
			</scroll-view>
		</view>
		
		<!-- 内容列表 -->
		<scroll-view scroll-y class="content-scroll" refresher-enabled @refresherrefresh="onRefresh" :refresher-triggered="isRefreshing">
			<view class="collection-list" v-if="collectionList.length > 0">
				<view 
					class="collection-card"
					v-for="(item, index) in collectionList" 
					:key="index"
					:class="{'card-shadow': !isEditMode}"
					:style="{animationDelay: index * 0.05 + 's'}"
				>
					<!-- 选择框（编辑模式显示） -->
					<view class="select-box" v-if="isEditMode" @click="toggleSelect(index)">
						<view class="circle" :class="{selected: item.selected}">
							<up-icon v-if="item.selected" name="checkmark" size="12" color="#ffffff"></up-icon>
						</view>
					</view>
					
					<!-- 卡片内容 -->
					<view class="card-content" @click="viewDetail(item)">
						<image class="card-image" :src="item.cover" mode="aspectFill"></image>
						<view class="card-info">
							<text class="card-title">{{item.title}}</text>
							<view class="card-meta">
								<text class="meta-tag">{{item.category}}</text>
								<text class="meta-date">{{item.date}}</text>
							</view>
							<text class="card-desc">{{item.description}}</text>
						</view>
					</view>
				</view>
			</view>
			
			<!-- 空状态 -->
			<view class="empty-state" v-else>
				<image class="empty-image" src="/static/user-img.jpg" mode="aspectFit"></image>
				<text class="empty-text">当前分类暂无收藏内容</text>
				<view class="empty-action" @click="goExplore">
					<text>去探索</text>
				</view>
			</view>
		</scroll-view>
		
		<!-- 底部操作栏（编辑模式显示） -->
		<view class="bottom-actions" v-if="isEditMode">
			<view class="action-btn select-all" @click="selectAll">
				<view class="circle" :class="{selected: isAllSelected}">
					<up-icon v-if="isAllSelected" name="checkmark" size="12" color="#ffffff"></up-icon>
				</view>
				<text>全选</text>
			</view>
			<view class="action-btn delete-btn" :class="{disabled: !hasSelected}" @click="deleteSelected">
				<text>删除</text>
			</view>
		</view>
	</view>
</template>

<script setup>
import { ref, computed, reactive } from 'vue';

// 标签数据
const tabs = reactive([
	{ name: '全部', count: 12 },
	{ name: '文章', count: 5 },
	{ name: '视频', count: 3 },
	{ name: '音频', count: 2 },
	{ name: '文档', count: 2 }
]);

const activeTab = ref(0);
const isEditMode = ref(false);
const isRefreshing = ref(false);

// 收藏数据
const collectionList = reactive([
	{
		id: 1,
		title: '前端性能优化实践指南',
		description: '本文介绍了前端性能优化的核心方法和实战技巧，帮助你的应用速度提升50%。',
		category: '技术文章',
		date: '3天前',
		cover: '/static/user-img.jpg',
		selected: false
	},
	{
		id: 2,
		title: 'Vue3组件设计模式详解',
		description: '深入探讨Vue3中的高级组件设计模式，掌握组件化开发的最佳实践。',
		category: '技术文章',
		date: '1周前',
		cover: '/static/user-img.jpg',
		selected: false
	},
	{
		id: 3,
		title: 'CSS Grid完全指南',
		description: '从基础到高级，全面掌握CSS Grid布局系统，轻松实现复杂网格布局。',
		category: '教程',
		date: '2周前',
		cover: '/static/user-img.jpg',
		selected: false
	}
]);

// 计算属性
const totalItems = computed(() => collectionList.length);
const isAllSelected = computed(() => collectionList.length > 0 && collectionList.every(item => item.selected));
const hasSelected = computed(() => collectionList.some(item => item.selected));

// 方法
const switchTab = (index) => {
	activeTab.value = index;
	// 在实际应用中此处应该根据tab请求不同的数据
};

const toggleEditMode = () => {
	isEditMode.value = !isEditMode.value;
	// 退出编辑模式时清除所有选择
	if (!isEditMode.value) {
		collectionList.forEach(item => item.selected = false);
	}
};

const toggleSelect = (index) => {
	collectionList[index].selected = !collectionList[index].selected;
};

const selectAll = () => {
	const newState = !isAllSelected.value;
	collectionList.forEach(item => item.selected = newState);
};

const deleteSelected = () => {
	if (!hasSelected.value) return;
	
	uni.showModal({
		title: '确认删除',
		content: '是否删除选中的收藏内容？',
		success: (res) => {
			if (res.confirm) {
				// 从数组中移除选中项
				const newList = collectionList.filter(item => !item.selected);
				collectionList.length = 0;
				newList.forEach(item => collectionList.push(item));
				
				uni.showToast({
					title: '删除成功',
					icon: 'success'
				});
			}
		}
	});
};

const viewDetail = (item) => {
	if (isEditMode.value) return;
	// 跳转到详情页
	uni.showToast({
		title: '查看详情: ' + item.title,
		icon: 'none'
	});
};

const onRefresh = () => {
	isRefreshing.value = true;
	setTimeout(() => {
		isRefreshing.value = false;
		uni.showToast({
			title: '刷新成功',
			icon: 'success'
		});
	}, 1000);
};

const goExplore = () => {
	uni.switchTab({
		url: '/pages/index/index'
	});
};
</script>

<style lang="scss">
.collection-container {
	min-height: 100vh;
	background-color: #f5f7fa;
	display: flex;
	flex-direction: column;
	position: relative;
	overflow-x: hidden;
	box-sizing: border-box;
	width: 100%;
}

/* 顶部区域样式 */
.header {
	padding: 30rpx 40rpx;
	display: flex;
	justify-content: space-between;
	align-items: center;
	background-color: #fff;
	position: relative;
	box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.05);
	z-index: 2;
	width: 100%;
	box-sizing: border-box;
	
	&::after {
		content: '';
		position: absolute;
		bottom: 0;
		left: 30rpx;
		right: 30rpx;
		height: 1px;
		background-color: rgba(220, 220, 220, 0.3);
	}
}

.header-title {
	display: flex;
	flex-direction: column;
	flex: 1;
	min-width: 0;
}

.title-text {
	font-size: 36rpx;
	font-weight: 600;
	color: #2b2b2b;
	position: relative;
	display: inline-block;
	white-space: nowrap;
	overflow: hidden;
	text-overflow: ellipsis;
	
	&::after {
		content: '';
		position: absolute;
		bottom: -8rpx;
		left: 0;
		width: 60rpx;
		height: 6rpx;
		border-radius: 3rpx;
		background: linear-gradient(90deg, #3c9cff, #5cadff);
	}
}

.sub-title {
	font-size: 24rpx;
	color: #909399;
	margin-top: 16rpx;
	white-space: nowrap;
	overflow: hidden;
	text-overflow: ellipsis;
}

.header-action {
	font-size: 28rpx;
	color: #3c9cff;
	padding: 10rpx 24rpx;
	border-radius: 30rpx;
	background-color: rgba(60, 156, 255, 0.1);
	transition: all 0.3s;
	box-shadow: 0 4rpx 12rpx rgba(60, 156, 255, 0.1);
	margin-left: 20rpx;
	min-width: 90rpx;
	text-align: center;
	
	&:active {
		background-color: rgba(60, 156, 255, 0.2);
		transform: translateY(2rpx);
	}
}

/* 分类选项卡样式 */
.category-tabs {
	background-color: #fff;
	padding: 20rpx 0 30rpx;
	position: sticky;
	top: 0;
	z-index: 1;
	box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.03);
	width: 100%;
	box-sizing: border-box;
}

.tabs-scroll {
	white-space: nowrap;
	padding: 0 20rpx;
	width: 100%;
	box-sizing: border-box;
}

.tab-item {
	display: inline-block;
	padding: 16rpx 34rpx;
	margin: 0 10rpx;
	font-size: 28rpx;
	color: #606266;
	border-radius: 40rpx;
	transition: all 0.3s;
	position: relative;
	overflow: hidden;
	
	&.active {
		color: #fff;
		background: linear-gradient(90deg, #3c9cff, #5cadff);
		font-weight: 500;
		box-shadow: 0 6rpx 16rpx rgba(60, 156, 255, 0.2);
	}
	
	&:first-child {
		margin-left: 0;
	}
	
	&:not(.active):active {
		background-color: #f5f5f5;
	}
}

/* 内容列表样式 */
.content-scroll {
	flex: 1;
	padding: 30rpx 30rpx;
	width: 100%;
	box-sizing: border-box;
}

.collection-list {
	padding-bottom: 120rpx;
	width: 100%;
}

.collection-card {
	margin-bottom: 30rpx;
	background-color: #fff;
	border-radius: 20rpx;
	overflow: hidden;
	display: flex;
	animation: fadeInUp 0.5s ease forwards;
	transform: translateY(30rpx);
	opacity: 0;
	width: 100%;
	box-sizing: border-box;
	
	&.card-shadow {
		box-shadow: 0 8rpx 30rpx rgba(0, 0, 0, 0.08);
		border: 1rpx solid rgba(235, 238, 245, 0.8);
		
		&:active {
			transform: scale(0.98);
			transition: transform 0.2s;
		}
	}
}

@keyframes fadeInUp {
	to {
		opacity: 1;
		transform: translateY(0);
	}
}

.select-box {
	padding: 0 20rpx;
	display: flex;
	align-items: center;
	min-width: 60rpx;
}

.circle {
	width: 40rpx;
	height: 40rpx;
	border-radius: 50%;
	border: 2rpx solid #ddd;
	display: flex;
	align-items: center;
	justify-content: center;
	min-width: 40rpx;
	
	&.selected {
		background: linear-gradient(135deg, #3c9cff, #5cadff);
		border-color: #3c9cff;
	}
}

.card-content {
	display: flex;
	flex: 1;
	padding: 24rpx 24rpx 24rpx 0;
	width: calc(100% - 60rpx);
	box-sizing: border-box;
	overflow: hidden;
}

.card-image {
	width: 200rpx;
	height: 180rpx;
	border-radius: 16rpx;
	margin-right: 24rpx;
	background-color: #f5f5f5;
	object-fit: cover;
	box-shadow: 0 6rpx 12rpx rgba(0, 0, 0, 0.1);
	min-width: 160rpx;
	flex-shrink: 0;
}

.card-info {
	flex: 1;
	display: flex;
	flex-direction: column;
	justify-content: space-between;
	min-width: 0;
	overflow: hidden;
}

.card-title {
	font-size: 32rpx;
	font-weight: 600;
	color: #2b2b2b;
	line-height: 1.4;
	margin-bottom: 12rpx;
	width: 100%;
	overflow: hidden;
	text-overflow: ellipsis;
	white-space: nowrap;
}

.card-meta {
	display: flex;
	justify-content: space-between;
	align-items: center;
	margin-bottom: 16rpx;
	width: 100%;
	flex-wrap: wrap;
}

.meta-tag {
	font-size: 24rpx;
	font-weight: 500;
	color: #3c9cff;
	background-color: rgba(60, 156, 255, 0.1);
	padding: 6rpx 20rpx;
	border-radius: 30rpx;
	border: 1rpx solid rgba(60, 156, 255, 0.2);
	max-width: 160rpx;
	overflow: hidden;
	text-overflow: ellipsis;
	white-space: nowrap;
}

.meta-date {
	font-size: 24rpx;
	color: #909399;
}

.card-desc {
	font-size: 26rpx;
	color: #606266;
	line-height: 1.6;
	overflow: hidden;
	text-overflow: ellipsis;
	display: -webkit-box;
	-webkit-line-clamp: 2;
	-webkit-box-orient: vertical;
	width: 100%;
}

/* 空状态样式 */
.empty-state {
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
	padding: 120rpx 0;
	width: 100%;
	box-sizing: border-box;
}

.empty-image {
	width: 240rpx;
	height: 240rpx;
	margin-bottom: 40rpx;
	border-radius: 50%;
	object-fit: cover;
	opacity: 0.8;
	box-shadow: 0 10rpx 30rpx rgba(0, 0, 0, 0.1);
}

.empty-text {
	font-size: 30rpx;
	color: #909399;
	margin-bottom: 50rpx;
	text-align: center;
	width: 100%;
	padding: 0 30rpx;
	box-sizing: border-box;
}

.empty-action {
	padding: 20rpx 70rpx;
	background: linear-gradient(135deg, #46a6ff, #3c9cff);
	border-radius: 50rpx;
	color: #fff;
	font-size: 30rpx;
	box-shadow: 0 10rpx 20rpx rgba(60, 156, 255, 0.25);
	letter-spacing: 2rpx;
	transition: all 0.3s;
	
	&:active {
		transform: translateY(4rpx);
		box-shadow: 0 6rpx 10rpx rgba(60, 156, 255, 0.2);
	}
}

/* 底部操作栏样式 */
.bottom-actions {
	position: fixed;
	bottom: 0;
	left: 0;
	right: 0;
	height: 120rpx;
	background-color: #fff;
	display: flex;
	align-items: center;
	padding: 0 40rpx;
	box-shadow: 0 -4rpx 16rpx rgba(0, 0, 0, 0.08);
	z-index: 10;
	width: 100%;
	box-sizing: border-box;
}

.action-btn {
	display: flex;
	align-items: center;
}

.select-all {
	flex: 1;
	text {
		margin-left: 12rpx;
		font-size: 30rpx;
		color: #333;
	}
}

.delete-btn {
	padding: 18rpx 70rpx;
	background: linear-gradient(135deg, #ff6e6e, #fa3534);
	border-radius: 50rpx;
	box-shadow: 0 8rpx 16rpx rgba(250, 53, 52, 0.2);
	transition: all 0.3s;
	min-width: 200rpx;
	text-align: center;
	
	text {
		font-size: 30rpx;
		color: #fff;
		letter-spacing: 2rpx;
	}
	
	&.disabled {
		background: linear-gradient(135deg, #ffb1b1, #ffcaca);
		opacity: 0.8;
		box-shadow: none;
	}
	
	&:not(.disabled):active {
		transform: translateY(3rpx);
		box-shadow: 0 4rpx 8rpx rgba(250, 53, 52, 0.15);
	}
}
</style> 